#include "Message.hpp"

Message::Message(): m_body_length(0)
{

}

void Message::body_length(std::size_t new_length)
{
    m_body_length = new_length;
    if(m_body_length > max_body_length)
        m_body_length = max_body_length;
}

bool Message::decode_header()
{
    char header[head_length + 1] = "";
    std::strncat(header, m_data, head_length); // Copy head Data inside m_data length is head_length;
    m_body_length = std::atoi(header);
    if(m_body_length > max_body_length)
    {
        m_body_length = 0;
        return false;
    }
    return true;
}


void Message::encode_header()
{
    char header[head_length + 1] = "";
    std::sprintf(header, "%4d", static_cast<int>(m_body_length));
    std::memcpy(m_data, header, head_length);
}
















